package com.liuhousemyjavabasic.controller;

import com.baidu.aip.ocr.AipOcr;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.MediaType;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;

@RestController
@Slf4j
@RequestMapping("baidu_api")
//@ConfigurationProperties(prefix = "baidu")
public class BaiduOcrController {
    @Value("${baidu.appid}")
    private String APP_ID;
    @Value("${baidu.api_key}")
    private String API_KEY;
    @Value("${baidu.secret_key}")
    private String SECRET_KEY;

    @PostMapping(value = "recognize" , consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String recognize(@RequestParam("file") MultipartFile file) throws IOException {
        //初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        //调用接口
        JSONObject res = client.basicGeneral(file.getBytes(), new HashMap<String, String>());
        return res.toString(2);
    }


}
